package com.abcdefg.demo.work;

import com.abcdefg.demo.constants.Constants;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class WorkConsume2 {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.Host);//ip 默认值localhost
        factory.setPort(Constants.Port); //默认值5672
        factory.setVirtualHost(Constants.VirtualHost);//虚拟机名称,
        factory.setUsername(Constants.UserName);//⽤⼾名
        factory.setPassword(Constants.Password);//密码
        //3. 创建连接Connection
        Connection connection = factory.newConnection();
        //4. 创建channel通道
        Channel channel = connection.createChannel();
        //5.声明一个队列
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);
        DefaultConsumer consumer=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息: " + new String(body));
            }
        };

        channel.basicConsume(Constants.WORK_QUEUE,true,consumer);

    }
}
